package cn.com.nes.site.service.product.impl;

import cn.com.nes.common.util.CopyUtils;
import cn.com.nes.mybatis.agent.ope.entity.ProductFieldsConfig;
import cn.com.nes.mybatis.agent.ope.mapper.ProductFieldsConfigMapper;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.dto.product.ProductFieldsConfigDTO;
import cn.com.nes.site.entity.vo.product.ProductFieldsConfigVO;
import cn.com.nes.site.service.product.GGoodsService;
import cn.com.nes.site.service.product.ProductFieldsConfigService;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static cn.com.nes.site.service.product.impl.GGoodsServiceImpl.GOODS_FIELDS_SBLX;

/**
 * Created with IntelliJ IDEA.
 */
@Service
public class ProductFieldsConfigServiceImpl extends ServiceImpl<ProductFieldsConfigMapper, ProductFieldsConfig> implements ProductFieldsConfigService {

    @Resource
    GGoodsService gGoodsService;

    @Resource
    ProductFieldsConfigMapper productFieldsConfigMapper;

    @Override
    public PageData getFieldsConfig(String equipmentTypeId) {
        // 组装查询参数
        LambdaQueryWrapper<ProductFieldsConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductFieldsConfig::getDeleted, false);
        wrapper.eq(ProductFieldsConfig::getEquipmentTypeId, equipmentTypeId);
        wrapper.orderByDesc(ProductFieldsConfig::getId);
        List<ProductFieldsConfig> configs = this.baseMapper.selectList(wrapper);
        // 转换查询结果为Map
        Map<String, Object> map = CopyUtils.listToMap("configList", configs, ProductFieldsConfigDTO.class);
        return PageData.ok(map);
    }

    @Override
    public PageData saveFieldsConfig(ProductFieldsConfigVO vo) {
        // 校验字段配置是否重复
        if (checkFieldConfig(vo)) {
            return PageData.error("保存失败,当前字段已被占用");
        }

        ProductFieldsConfig productFieldsConfig = new ProductFieldsConfig();
        BeanUtils.copyProperties(vo, productFieldsConfig);

        Long id = productFieldsConfig.getId();
        Long userId = Long.valueOf(vo.getUserInfo().getId());

        // 新增
        if (Objects.isNull(id)) {
            productFieldsConfig.setDeleted(false);
            productFieldsConfig.setGmtCreate(new Date());
            productFieldsConfig.setGmtCreateId(userId);
        } else {
            // 校验是否可以修改
            if (!Objects.isNull(vo.getId())) {
                ProductFieldsConfig config = productFieldsConfigMapper.selectById(id);
                Boolean canUpdate = gGoodsService.checkCanUpdate(GOODS_FIELDS_SBLX, config.getEquipmentTypeId());
                if (!canUpdate) {
                    return PageData.error("修改失败,当前字段对应规则正在使用中!");
                }
            }
            productFieldsConfig.setGmtModified(new Date());
            productFieldsConfig.setGmtModifiedId(userId);
        }

        this.saveOrUpdate(productFieldsConfig);
        return PageData.ok("保存成功");
    }

    @Override
    public PageData delFieldsConfig(ProductFieldsConfigVO vo) {
        // 校验是否可以删除
        ProductFieldsConfig config = productFieldsConfigMapper.selectById(vo.getId());
        Boolean canUpdate = gGoodsService.checkCanUpdate(GOODS_FIELDS_SBLX, config.getEquipmentTypeId());
        if (!canUpdate) {
            return PageData.error("删除失败,当前字段对应规则正在使用中!");
        }
        // 逻辑删除操作
        ProductFieldsConfig productFieldsConfig = new ProductFieldsConfig();
        productFieldsConfig.setId(vo.getId());
        productFieldsConfig.setDeleted(true);
        productFieldsConfig.setGmtModified(new Date());
        productFieldsConfig.setGmtModifiedId(Long.valueOf(vo.getUserInfo().getId()));

        productFieldsConfigMapper.updateById(productFieldsConfig);
        return PageData.ok("删除完成");
    }

    @Override
    public PageData copyFieldsConfig(ProductFieldsConfigVO vo) {
        String newEquipmentTypeId = vo.getNewEquipmentTypeId();
        String oldEquipmentTypeId = vo.getOldEquipmentTypeId();
        Long userId = Long.valueOf(vo.getUserInfo().getId());

        // 查询已有产品的字段配置
        LambdaQueryWrapper<ProductFieldsConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductFieldsConfig::getDeleted, false);
        wrapper.eq(ProductFieldsConfig::getEquipmentTypeId, oldEquipmentTypeId);
        wrapper.orderByDesc(ProductFieldsConfig::getId);
        List<ProductFieldsConfig> configs = this.baseMapper.selectList(wrapper);
        // 转换查询结果类型
        List<ProductFieldsConfigDTO> configList = CopyUtils.copyList(configs, ProductFieldsConfigDTO.class);
        if (CollectionUtil.isEmpty(configList)) {
            return PageData.ok("复制完成");
        }
        // 复制这些字段配置
        for (ProductFieldsConfigDTO configDTO : configList) {
            configDTO.setId(null);
            configDTO.setEquipmentTypeId(null);
            configDTO.setGmtCreate(null);
            configDTO.setGmtCreateId(null);
            configDTO.setGmtModified(null);
            configDTO.setGmtModifiedId(null);

            ProductFieldsConfig productFieldsConfig = new ProductFieldsConfig();
            BeanUtils.copyProperties(configDTO, productFieldsConfig);
            productFieldsConfig.setEquipmentTypeId(newEquipmentTypeId);
            productFieldsConfig.setDeleted(false);
            productFieldsConfig.setGmtCreate(new Date());
            productFieldsConfig.setGmtCreateId(userId);

            productFieldsConfigMapper.insert(productFieldsConfig);
        }
        return PageData.ok("复制完成");
    }

    /**
     * 校验字段配置是否重复
     * @param vo
     * @return true-不能保存  false-可以保存
     */
    public Boolean checkFieldConfig(ProductFieldsConfigVO vo) {
        Long id = vo.getId();
        String equipmentTypeId = vo.getEquipmentTypeId();
        String goodsFields = vo.getGoodsFields();

        // 查询字段是否被占用
        LambdaQueryWrapper<ProductFieldsConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductFieldsConfig::getDeleted, false);
        wrapper.eq(ProductFieldsConfig::getEquipmentTypeId, equipmentTypeId);
        wrapper.eq(ProductFieldsConfig::getGoodsFields, goodsFields);
        Integer count = this.baseMapper.selectCount(wrapper);

        // 没被占用
        if (0 == count) {
            return false;
        }

        // 已被占用
        // 如果是新增
        if (Objects.isNull(id)) {
            return true;
        }
        // 如果是修改
        ProductFieldsConfig productFieldsConfig = productFieldsConfigMapper.selectById(id);
        String oldFields = productFieldsConfig.getGoodsFields();
        if (oldFields.equals(goodsFields)) {
            return false;
        }
        return true;
    }

}
